<?php
class IndexController extends Zend_Controller_Action
{	
    function init()
    {
		$this->initView();		
        $this->view->baseUrl = $this->_request->getBaseUrl();
 		$this->view->title = 'plima管理系统';
		//加载日志
		$this->logger=Zend_Registry::get("logger");	
		$this->view->user = (array)Zend_Auth::getInstance()->getIdentity();
		$this->db=Zend_Registry::get('dbAdapter');
		if(!empty($this->view->user)){			
			$this->view->role=$this->view->user['role'];
			//echo $this->view->role;exit;
			$select=$this->db->select();
			$select->from('admin_user','*');			
			$select->join('plm_role','admin_user.role=plm_role.role_name','*');
			$select->where('admin_user.username = ?',$this->view->user['username']);
			//echo $select->__toString();
			//exit;
			$result=$this->db->fetchRow($select);
			//print_r($result);exit;
			$acl=unserialize($result['role_acl']);
//			$acl->add(new Zend_Acl_Resource("admin"));
//			$acl->add(new Zend_Acl_Resource("product"));
//			$acl->add(new Zend_Acl_Resource("order"));
			//print_r($acl);exit;
			//print_r($acl);exit;
			//$this->view->acl=Zend_Registry::get('acl');
			$this->view->acl=$acl;
		}		
    }
 
   function indexAction() 
    { 	$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
		$this->_redirect('/index/main');
		}
		$this->render();
		//print_r($_SESSION['Auth_Code']);
    } 
 	
	function welcomeAction(){
		exit;
		$this->render();
	}
	
	function imgcodeAction()
	{
		Zend_Loader::loadClass('Custom_Controller_Plugin_ImgCode');
		$imagecode=new Custom_Controller_Plugin_ImgCode();    	
    	$imagecode->image2(4,1);
	} 
	
	function loginAction(){
		//Zend_Loader::loadClass('Session_Namespace');
		require_once 'Zend/Session/Namespace.php';
		if($this->_request->isPost()){
			Zend_Loader::loadClass('Zend_Filter_StripTags');
    		$filter=new Zend_Filter_StripTags();
    		$username=$filter->filter($this->_request->getPost('username'));
    		$password=md5($filter->filter($this->_request->getPost('password')));
    		$imgcode=$filter->filter($this->_request->getPost('imgcode'));
    		$authCode = new Zend_Session_Namespace('Auth_Code');
			if(strtolower($authCode->imagecode)==strtolower($imgcode)){
				if($username!='' && $password!=''){
					zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
					$dbAdapter=Zend_Registry::get('dbAdapter');
	    			$authAdapter=new Zend_Auth_Adapter_DbTable($dbAdapter);
	    			$authAdapter->setTableName('admin_user');
	    			$authAdapter->setIdentityColumn('username');
	    			$authAdapter->setCredentialColumn('password');
					$authAdapter->setIdentity($username);
	    			$authAdapter->setCredential($password);
	    			$auth=Zend_Auth::getInstance();
	    			$result=$auth->authenticate($authAdapter);
	    			if($result->isValid()){
	    				$data=$authAdapter->getResultRowObject(null,'password');
	    				$auth->getStorage()->write($data);
						$this->logger->info($username."登陆");
						success("OK");
	    			}else{
	    				failure('用户名密码不正确');
	    			}
					}
				else{
					failure('用户名密码不能为空');
					}
				}
			else{
				failure("验证码错误");			
				//failure("验证码".$imgcode."错误".$authCode->imagecode);			
				}
			}
		}
	
	function mainAction(){
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			Zend_Loader::loadclass('myacl');
			$this->view->myacl=new myacl($this->view->acl,$this->view->role);
			$this->view->title = 'plima管理系统';
			$this->render();
		}
		else{
			$this->_redirect('/');
			}
		}	
	function logoutAction(){
		Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/');
		}
		
	function menuorderAction(){
		
		}
	
	function menuproductAction(){
		
		}
		
	function menuadminAction(){
		
		}
	function menulinkAction(){
		
		}
	function chartsAction(){
		//图表测试用
		}
	function dataviewAction(){
		//ext3.2测试dataview
		}
	function desktopAction(){
		//测试ext3.2的桌面功能
		}
	function controlpageAction(){
		$id=$this->_request->getParam("id");
		$name=$this->_request->getParam("name");
		if(empty($id)){
			echo "error";exit;
		}
		$select=$this->db->select();
		$select->from('plm_role','*');
		$select->where('role_id = ?',$id);
		$result=$this->db->fetchRow($select);
		$acl=unserialize($result['role_acl']);
		//print_r($acl);exit;
		$this->view->acl=$acl;
		$this->view->role=$result['role_name'];
		$this->view->id=$id;
		$this->view->name=$name;
		}
	
	function __call($method,$args){
		if ('Action' == substr($method, -6)) 
    	{            
    		// If the action method was not found, forward to the index action            
    		return $this->_forward('index');        
    	}        
    	// all other methods throw an exception        
    	throw new Exception('Invalid method "' . $method . '" called');    
		}
}
?>